﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sedilizatoru
{

    public partial class Form1 : Form
    {
        public  bool Replace(string file, string searchFor, string replaceWith)
        {
            try
            {
                Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
                //get a StreamReader for reading the file
                StreamReader reader = new StreamReader(file, encoding);

                //read the entire file at once
                string contents = reader.ReadToEnd();

                //close up and dispose
                reader.Close();
                reader.Dispose();

                //use regular expressions to search and replace our text
                contents = Regex.Replace(contents, searchFor, replaceWith);
                
                //get a StreamWriter for writing the new text to the file
                // System.AppDomain.CurrentDomain.BaseDirectory.ToString() + textBox1.Text
                // string path = fdlg.FileName;
                string path = ofd.FileName;
                
                StreamWriter writer = new StreamWriter(path, false, Encoding.Unicode);
                StreamWriter writer2 = new StreamWriter(path + ".sub", false, Encoding.Unicode);

                //write the contents
                writer.Write(contents);
                writer2.Write(contents);

                //close up and dispose
                writer.Close();
                writer.Dispose();
                writer2.Close();
                writer2.Dispose();

                //return successful
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /*public bool Culoare(string file, string color)
        {
            try
            {
                Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
                //get a StreamReader for reading the file
                StreamReader reader = new StreamReader(file, encoding);

                //read the entire file at once
                string contents = reader.ReadToEnd();

                //close up and dispose
                reader.Close();
                reader.Dispose();

                //use regular expressions to search and replace our text
                //contents = Regex.Match(contents, searchFor, replaceWith);
                string pat = @"(([0]?[1-9]|1[0-2])(:)([0-5][0-9]))";
                Regex r = new Regex(pat, RegexOptions.IgnoreCase);

                Match m = r.Match(contents);
                string tag1 = "<font color='" + color + "'>";
                string tag2 = "</font>";
                StringBuilder sb = new StringBuilder();
                sb.Append(" ");
                sb.Append(tag1);
                int i = 0;
                sb.Append(m.Groups[i].Value); i++;
                sb.Append(tag2);
                sb.Append(" ");
                string text = sb.ToString();
                contents = r.Replace(contents, text);
                
                //get a StreamWriter for writing the new text to the file
                // System.AppDomain.CurrentDomain.BaseDirectory.ToString() + textBox1.Text
                // string path = fdlg.FileName;
                string path = ofd.FileName;

                StreamWriter writer = new StreamWriter(path, false, Encoding.Unicode);
                StreamWriter writer2 = new StreamWriter(path + ".sub", false, Encoding.Unicode);

                //write the contents
                writer.Write(contents);
                writer2.Write(contents);

                //close up and dispose
                writer.Close();
                writer.Dispose();
                writer2.Close();
                writer2.Dispose();

                //return successful
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }*/

        /*public bool Bold(string file, string pat)
        {
            try
            {
                Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
                StreamReader reader = new StreamReader(file, encoding);

                string contents = reader.ReadToEnd();

                reader.Close();
                reader.Dispose();

                string tag1 = "<b>";
                string tag2 = "</b>";
                
                StringBuilder sb = new StringBuilder(); 
                sb.Append(" ");
                sb.Append(tag1);
                sb.Append("$1");
                sb.Append(tag2);
                sb.Append(" ");
                string text = sb.ToString();
                contents = Regex.Replace(contents, pat, text);

                string path = ofd.FileName;

                StreamWriter writer = new StreamWriter(path, false, Encoding.Unicode);
                StreamWriter writer2 = new StreamWriter(path + ".sub", false, Encoding.Unicode);
                
                writer.Write(contents);
                writer2.Write(contents);

                writer.Close();
                writer.Dispose();
                writer2.Close();
                writer2.Dispose();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }*/

        OpenFileDialog ofd = new OpenFileDialog();
       
        public Form1()
        {

            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            
            ofd.Title = "Cauta subtitrarea";
            ofd.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
            ofd.Filter = "Text Files(*.txt)|*.txt|SUB Files(*.sub)|*.sub|SRT Files(*.srt)|*.srt";
            ofd.FilterIndex = 3;
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string path = ofd.FileName;
                string filename = Path.GetFileName(path);
                textBox1.Text = filename;
            }
        }      

        private void button2_Click(object sender, EventArgs e)
        {
            string path = ofd.FileName;
            if (checkBox1.CheckState == CheckState.Checked)
            {
                Replace(path, "ã", "ă");
                Replace(path, "º", "ș");
                Replace(path, "þ", "ț");
                Replace(path, "ª", "Ș");
                Replace(path, "Þ", "Ț");
                Replace(path, "Ã", "Ă");
                Replace(path, "║", "ș");
                Replace(path, "π", "ă");
                Replace(path, "■", "ț");
                Replace(path, "ε", "â");
            }
            if (checkBox2.CheckState == CheckState.Checked)
            {
                Replace(path, "ã", "a");
                Replace(path, "º", "s");
                Replace(path, "þ", "t");
                Replace(path, "ª", "S");
                Replace(path, "Þ", "T");
                Replace(path, "Ã", "A");
                Replace(path, "║", "s");
                Replace(path, "π", "a");
                Replace(path, "■", "t");
                Replace(path, "ε", "a");
                Replace(path, "â", "a");
                Replace(path, "î", "i");
                Replace(path, "ă", "a");
                Replace(path, "ș", "s");
                Replace(path, "ț", "t");
                Replace(path, "Ș", "S");
                Replace(path, "Ț", "T");
                Replace(path, "Ă", "A");
                Replace(path, "Â", "s");
                Replace(path, "Î", "i");
            }
        }
    }
}
